<?php

namespace App\Console\Commands;

use App\Models\Troops;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Redis;

class Record extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'record';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $users = User::all();

        foreach ($users as $key => $val) {
            $item = $users->firstWhere('number', $val->nickname);
            if ($item) {
                $val->openid = $item->sex;
                $res = $val->save();
                if ($res) {
                    echo '处理完' . $val->number;
                } else {
                    echo '处理失败';
                }
            }
        }
exit;
        /*
        $troopsNumber = Redis::get("user:troops:number:$firendNumber");
        $isTroops = Redis::get("troops:$troopsNumber");
        if ($isTroops) {
            return Tools::error($firendNumber . '已有队伍');
        }
        Redis::set('troops:' . $data['number'], serialize($troopsData));
        Redis::set('user:troops:number:' . $number, $data['number']);
        Redis::set('user:troops:number:' . $firendNumber, $data['number']);
        */
        echo $users->count();
        $isn = 31;
        foreach ($users as $key => $val) {
            // 如果没有性别
            if (!$val->firend_sex && !$val->firend_number) {
                $val->firend_sex = 3;
            }
            if ($val->id > 120) {
                exit;
            } else {
                $val->number = ++$isn;
//                $uid = ++$isn;
//                echo $uid;
            }

            // 获取他的编号
//            $userInfo = unserialize(Redis::get('user:' . $uid));
//
//            $val->number = $userInfo['number'];

            // 队伍编号
            $troopsInfo = Troops::where('user_id', $val->number)->orWhere('cooperate_id', $val->number)->first();

            if ($troopsInfo) {
                $val->goal = $troopsInfo->number;

                if ($troopsInfo->cooperate_id != $val->number) {
                    $val->nickname = $troopsInfo->cooperate_id;
                } else {
                    $val->nickname = $troopsInfo->user_id;
                }

                $res = $val->save();
                if ($res) {
                    echo '处理完' . $val->number;
                } else {
                    echo '处理失败';
                }
            }
        }


    }
}
